data= load('data.mat');
a_TX = data.a_TX;
a_RX =data.a_RX;
Fbb = data.Fbb;
H_c=data.H_c;
M = 4;
sigma = 0.001;
% Fbb = [0.966353075228383,-0.00430210063213045,-0.0777373297259928,0.0439954942081816,-0.00280494664573534,0.000240425190469937,-0.00432451395432430,0.000112535688521881,-0.164434431374126,0.0787548862149491,-0.0643666592087163,-0.0631841308607062,-0.0208153766589651,0.0707889602044578,0.0426223025513916,0.000908990230324828;
%     -0.00430210063213045,0.00104318329808140,-0.0108954978460209,0.00498991689665900,0.000240425190469937,0.999526177729683,-0.000247393363668325,-9.96870701063867e-06,0.0787548862149491,-0.00598772933810907,0.0270461714221034,0.106244759684196,0.0707889602044578,0.0439069474368598,-0.310824084749368,-0.00392115616232238;
%     -0.0777373297259928,-0.0108954978460209,-0.187245314851757,0.105733793367539,-0.00432451395432430,-0.000247393363668325,-0.00725729370939598,-0.000675894699956719,-0.0643666592087163,0.0270461714221034,0.975651152550929,-0.0276304611585282,0.0426223025513916,-0.310824084749368,0.0757728163940151,-0.00235979996969017;
%     0.0439954942081816,0.00498991689665900,0.105733793367539,-0.0573949610661607,0.000112535688521881,-9.96870701063867e-06,-0.000675894699956719,-0.00175865531580018,-0.0631841308607062,0.106244759684196,-0.0276304611585282,0.0162153638966679,0.000908990230324828,-0.00392115616232238,-0.00235979996969017,0.999782236880486];

for u=1:1:M
    Frf(:,u)=a_TX(:,u);
    Wrf(:,u)=a_RX(:,u);
end

% Constructin the effective channels
for u=1:1:M
    Channel=zeros(16,64);
    Channel(:,:)= H_c(u,:,:);
    H(u,:)=Wrf(:,u)'*Channel*Frf ;    % Effective channels
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rate_HS = 0;
SNR = 1/sigma/M;

% for u = 1:M
%     Fbb_u = Fbb(:,(u-1)*M+1:u*M);
%     [x_vec, x_val] = eigs(Fbb_u);
%     Fbb_i = x_vec(:,1) * sqrt(x_val(1,1));
%     for j = 1:M
%      normlized_Fbb=Fbb_i/sqrt((Frf*Fbb_i)'*(Frf*Fbb_i));
%     end
%     Fbb(:,(u-1)*M+1:u*M) = normlized_Fbb* normlized_Fbb';
% end

for u= 1:M 
interference = 0;
for j = 1:M
    if j~= u
        interference = interference + abs(H(u,:)*Fbb(:,(j-1)*M+1:j*M)*H(u,:)');
    end
end
SINR_HS=(SNR*(abs(H(u,:)*Fbb(:,(u-1)*M+1:u*M)*H(u,:)')))/(SNR*interference+1);
Rate_HS=Rate_HS+log2(1+SINR_HS);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fbb_new = zeros(M,M*M);
for u = 1:M
x_0 = Fbb(:,(u-1)*M+1:u*M);
[vec_x, val_x] = svd(x_0);
Fbb_new(:,(u-1)*M+1:u*M) = vec_x(:,1)*vec_x(:,1)';
end

Rate_HS_0 = 0;
SNR = 1/sigma;
for u= 1:M 
interference = 0;
for j = 1:M
    if j~= u
        interference = interference + abs(H(u,:)*Fbb_new(:,(j-1)*M+1:j*M)*H(u,:)');
    end
end
SINR_HS_0=(SNR*(abs(H(u,:)*Fbb_new(:,(u-1)*M+1:u*M)*H(u,:)')))/(SNR*interference+1);
Rate_HS_0=Rate_HS_0+log2(1+SINR_HS_0);
end



